﻿package frEngine.shader
{
    import baseEngine.system.*;
    import com.adobe.utils.*;
    import flash.display3D.*;
    import flash.utils.*;

    public class Program3DBuilder extends Object
    {
        private var vertexBytesAssembler:AGALMiniAssembler;
        private var fragmentBytesAssembler:AGALMiniAssembler;
        public var callBackFun:Function;
        private var _sourceCode:String;

        public function Program3DBuilder()
        {
            this.vertexBytesAssembler = new AGALMiniAssembler(false);
            this.fragmentBytesAssembler = new AGALMiniAssembler(false);
            return;
        }// end function

        public function buildByNormal(param1:String, param2:String, param3:Dictionary, param4:Dictionary, param5:int, param6:int) : void
        {
            var _loc_7:RegExp = null;
            var _loc_13:String = null;
            var _loc_14:String = null;
            var _loc_15:String = null;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:* = "vt" + param5;
            var _loc_11:* = "ft" + param6;
            param5++;
            param6++;
            var _loc_12:int = 0;
            while (_loc_12 < 8)
            {
                
                _loc_14 = "vt" + _loc_12;
                if (param1.indexOf(_loc_14) != -1)
                {
                    _loc_7 = new RegExp(_loc_14, "gi");
                    param1 = param1.replace(_loc_7, "#" + (param5 + _loc_8));
                    _loc_8++;
                }
                _loc_12++;
            }
            param1 = param1.replace(/#""#/gi, "vt");
            _loc_12 = 0;
            while (_loc_12 < 8)
            {
                
                _loc_15 = "ft" + _loc_12;
                if (param2.indexOf(_loc_15) != -1)
                {
                    _loc_7 = new RegExp(_loc_15, "gi");
                    param2 = param2.replace(_loc_7, "#" + (param6 + _loc_9));
                    _loc_9++;
                }
                _loc_12++;
            }
            param2 = param2.replace(/#""#/gi, "ft");
            if (param5 + _loc_8 > 8 || param6 + _loc_9 > 8)
            {
                throw new Error("临时变量个数大于8<FrProgram3D.as>");
            }
            param2 = param2 + "mov           oc,{output}\n";
            _loc_7 = /\{output\}""\{output\}/gi;
            param1 = param1.replace(_loc_7, _loc_10);
            param2 = param2.replace(_loc_7, _loc_11);
            for (_loc_13 in param3)
            {
                
                _loc_7 = new RegExp(_loc_13, "gi");
                param1 = param1.replace(_loc_7, param3[_loc_13]);
            }
            for (_loc_13 in param4)
            {
                
                _loc_7 = new RegExp(_loc_13, "gi");
                param2 = param2.replace(_loc_7, param4[_loc_13]);
            }
            param1 = Device3D.replaceVertexCode(param1);
            param2 = Device3D.replaceFragmentCode(param2);
            this._sourceCode = "vertexCode:\n" + param1 + "\n\nfragmentCode:\n" + param2;
            this.vertexBytesAssembler.assemble(Context3DProgramType.VERTEX, param1, 1, false);
            this.fragmentBytesAssembler.assemble(Context3DProgramType.FRAGMENT, param2, 1, false);
            if (this.vertexBytesAssembler.hasFinish && this.fragmentBytesAssembler.hasFinish)
            {
                this.assemblerFinish();
            }
            return;
        }// end function

        public function assemblerGoOn(param1:Boolean, param2:Number) : void
        {
            if (this.vertexBytesAssembler.hasFinish && this.fragmentBytesAssembler.hasFinish)
            {
                this.assemblerFinish();
            }
            return;
        }// end function

        private function assemblerFinish() : void
        {
            this.vertexBytesAssembler.hasFinish = false;
            this.fragmentBytesAssembler.hasFinish = false;
            if (this.callBackFun != null)
            {
                this.callBackFun(this.vertexBytesAssembler.agalcode, this.fragmentBytesAssembler.agalcode, this._sourceCode);
            }
            this.callBackFun = null;
            return;
        }// end function

    }
}
